package com.ty.tour.common.utils;

import com.github.pagehelper.PageInfo;
import lombok.ToString;

import java.util.List;

@ToString
public class PagedResult {

    private int page;            // 当前页数
    private int total;            // 总页数
    private long records;        // 总记录数
    private List<?> rows;        // 每行显示的内容

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }

    public long getRecords() {
        return records;
    }

    public void setRecords(long records) {
        this.records = records;
    }

    public List<?> getRows() {
        return rows;
    }

    public void setRows(List<?> rows) {
        this.rows = rows;
    }

    //简单sql查询 插件自动分页
    public static PagedResult setPageResult(List<?> list, int page) {
        PageInfo<?> pageList = new PageInfo<>(list);

        PagedResult pagedResult = new PagedResult();
        pagedResult.setPage(page);
        pagedResult.setTotal(pageList.getPages());
        pagedResult.setRows(list);
        pagedResult.setRecords(pageList.getTotal());

        return pagedResult;
    }

    //复杂sql查询 手动分页
    public static PagedResult getPageResult(List<?> dataList, Integer page, Integer size) {
        PagedResult pagedResult = new PagedResult();
        pagedResult.setTotal(dataList.size() < size ? 1 : dataList.size() % size == 0 ? dataList.size() / size : dataList.size() / size + 1);
        pagedResult.setRecords(dataList.size());
        pagedResult.setRows(dataList.subList(size * (page - 1), page == pagedResult.getTotal() ? dataList.size() : size * page));
        pagedResult.setPage(page);
        return pagedResult;
    }
}
